package controller

import (
	"StrayAnimals/internal/model"
	"StrayAnimals/internal/service"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func UploadAnimal(c *gin.Context) {
	var userInput struct {
		Name        string `json:"name"`
		Age         int    `json:"age"`
		Type        string `json:"type"`
		Information string `json:"information"`
		Picture1    string `json:"picture1"`
		Picture2    string `json:"picture2"`
		Picture3    string `json:"picture3"`
		UploadedAt  string `json:"uploadedAt"`
		UploaderId  string `json:"uploaderId"`
		IsAdopted   bool   `json:"isAdopted"`
	}

	if err := c.ShouldBindJSON(&userInput); err != nil {
		c.JSON(400, gin.H{
			"code":    400,
			"message": "请求参数错误；" + err.Error(),
		})
		return
	}

	newAnimal := model.AnimalsInformation{
		ID:          uuid.New().String(),
		Name:        userInput.Name,
		Age:         userInput.Age,
		Type:        userInput.Type,
		Information: userInput.Information,
		Picture1:    userInput.Picture1,
		Picture2:    userInput.Picture2,
		Picture3:    userInput.Picture3,
		UploadedAt:  userInput.UploadedAt,
		UploaderID:  userInput.UploaderId,
		IsAdopted:   userInput.IsAdopted,
	}

	if err := service.CreateNewAnimal(newAnimal); err != nil {
		c.JSON(500, gin.H{
			"code":    500,
			"message": "系统错误，上传动物失败，请稍后再试；" + err.Error(),
		})
		return
	}

	c.JSON(200, gin.H{
		"code":    200,
		"message": "上传动物成功",
	})
}
